package com.fare.fdc.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.xalan.lib.sql.QueryParameter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.analyse.AirCompanyTrendContrastFlightApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.flight.FlightInfoApplication;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.core.FlightInfoVO;
import com.fare.taluo.vo.core.SourceEntityVO;

/**
 * 
 * 【航空公司走势对比（航班）】
 * 
 * @version
 * @author guokenye
 * 
 */
@Controller("airCompanyTrendContrastFlightAction")
public class AirCompanyTrendContrastFlightAction extends BaseFdcAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1925822566596882358L;
	// =========================================================================================
	private String fromCityCode;
	private String toCityCode;
	private String flightStartDate;
	private String flightEndDate;
	private Integer futureDay;// 未来天数
	private String[] compareFlights;// 比较航班
	private String dataSourceHbaseKey;// 数据来源
	private String companyCarrierKey;// 在获取航空公司下航线的所有航班的时候需要用到的公司key
	@Resource(name = "airCompanyTrendContrastFlightApplicationImpl")
	private AirCompanyTrendContrastFlightApplication airCompanyTrendContrastFlightApplication;
	@Resource(name = "flightInfoApplication")
	private FlightInfoApplication flightInfoApplication;
	private List<SourceEntityVO> crawlCompanys;// 所有的当前处于抓取状态的公司
	private List<SourceEntityVO> crawlChannels;// 所有的当前处于抓取状态的渠道
	@Resource(name = "sourceApplication")
	private SourceApplication sourceApplication;

//	public void analyse() throws Exception {
//		String xml = airCompanyTrendContrastFlightApplication.getFusionChartsXMLStr(depCityName, arrCityName,flightStartDate, flightEndDate, futureDay,compareFlights, dataSourceHbaseKey);
//	
	
	public void analyse() throws Exception{
		Locale locale =super.getLocale();
		String xml=airCompanyTrendContrastFlightApplication.getFusionChartsXMLStr(fromCityCode, toCityCode, 
				flightStartDate, flightEndDate, futureDay, compareFlights, dataSourceHbaseKey,null,locale);
		super.toJSONOut(xml);
	}

	public String toAnalyseJSP() throws Exception {
		this.crawlCompanys = sourceApplication.findCrawlAirCompanys();
		this.crawlChannels = sourceApplication.findCrawlChannel();
		returnPageURL = "/fare/fdc/_zoushiduibiFlight.jsp";
		return "dispatcher";
	}

	/**
	 * 添加更多航班页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toAddFlightInfoJsp() throws Exception {
		this.crawlCompanys = sourceApplication.findCrawlAirCompanys();
		returnPageURL = "/fare/fdc/flightinfo.jsp";
		return "dispatcher";
	}

	/**
	 * 获取航线下的所有航班
	 * 
	 * @throws Exception
	 */
//	public void getFlightNo() throws Exception {
//		log.info("+++++++++" + companyCarrierKey + "-" + depCityName + "-"
//				+ arrCityName);
//		List<FlightInfoVO> vos = flightInfoApplication.findByConditions(
//				companyCarrierKey, depCityName, arrCityName);
		// vos=PriceUtil.generateOnly(vos);//排重
	public void getFlightNo() throws Exception{
		List<FlightInfoVO> vos = flightInfoApplication.findByLine(fromCityCode, toCityCode, companyCarrierKey);
		//航班号去重
		List<FlightInfoVO> distinctList = getDistinctList(vos);
		super.toJSONOut(JsonUtil.toJson(distinctList));
	}

	
	/**
	 * 根据航班号去重
	 * @param vos
	 * @return
	 */
	public static List<FlightInfoVO> getDistinctList(List<FlightInfoVO> vos){
		Map<String, FlightInfoVO> voMap = new HashMap<String, FlightInfoVO>();
		if(StringUtils.checkList(vos)){
			for (FlightInfoVO flightInfoVO : vos) {
				voMap.put(flightInfoVO.getFlightNo(), flightInfoVO);	
			}
		}
		List<FlightInfoVO> list = new ArrayList<FlightInfoVO>();
		list.addAll(voMap.values());
		
		return list;
	}
	
	
	public String getFromCityCode() {
		return fromCityCode;
	}


	public void setFromCityCode(String fromCityCode) {
		this.fromCityCode = fromCityCode;
	}

	public String getToCityCode() {
		return toCityCode;
	}

	public void setToCityCode(String toCityCode) {
		this.toCityCode = toCityCode;
	}

	public String getFlightStartDate() {
		return flightStartDate;
	}

	public void setFlightStartDate(String flightStartDate) {
		this.flightStartDate = flightStartDate;
	}

	public String getFlightEndDate() {
		return flightEndDate;
	}

	public void setFlightEndDate(String flightEndDate) {
		this.flightEndDate = flightEndDate;
	}

	public Integer getFutureDay() {
		return futureDay;
	}

	public void setFutureDay(Integer futureDay) {
		this.futureDay = futureDay;
	}

	public String[] getCompareFlights() {
		return compareFlights;
	}

	public void setCompareFlights(String[] compareFlights) {
		this.compareFlights = compareFlights;
	}

	public String getDataSourceHbaseKey() {
		return dataSourceHbaseKey;
	}

	public void setDataSourceHbaseKey(String dataSourceHbaseKey) {
		this.dataSourceHbaseKey = dataSourceHbaseKey;
	}

	public String getCompanyCarrierKey() {
		return companyCarrierKey;
	}

	public void setCompanyCarrierKey(String companyCarrierKey) {
		this.companyCarrierKey = companyCarrierKey;
	}

	public AirCompanyTrendContrastFlightApplication getAirCompanyTrendContrastFlightApplication() {
		return airCompanyTrendContrastFlightApplication;
	}

	public void setAirCompanyTrendContrastFlightApplication(
			AirCompanyTrendContrastFlightApplication airCompanyTrendContrastFlightApplication) {
		this.airCompanyTrendContrastFlightApplication = airCompanyTrendContrastFlightApplication;
	}

	public FlightInfoApplication getFlightInfoApplication() {
		return flightInfoApplication;
	}

	public void setFlightInfoApplication(
			FlightInfoApplication flightInfoApplication) {
		this.flightInfoApplication = flightInfoApplication;
	}

	public List<SourceEntityVO> getCrawlCompanys() {
		return crawlCompanys;
	}

	public void setCrawlCompanys(List<SourceEntityVO> crawlCompanys) {
		this.crawlCompanys = crawlCompanys;
	}

	public List<SourceEntityVO> getCrawlChannels() {
		return crawlChannels;
	}

	public void setCrawlChannels(List<SourceEntityVO> crawlChannels) {
		this.crawlChannels = crawlChannels;
	}

	public SourceApplication getSourceApplication() {
		return sourceApplication;
	}

	public void setSourceApplication(SourceApplication sourceApplication) {
		this.sourceApplication = sourceApplication;
	}
}